package com.alks.function.controller.biocontrol.deptwork;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.RoleCheck;
import com.alks.common.utils.DateUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.request.biocontrol.deptwork.PrepairRequest;
import com.alks.function.data.request.biocontrol.deptwork.PrepairTimeRequest;
import com.alks.function.service.biocontrol.deptwork.IBefPrepairService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@ApiRestController("/bioDeptWrok")
@Api(value = "生控作业", tags = "生控部-生控作业-产前计划安排")
@RequiredArgsConstructor
@RoleCheck(prgIds = "140104")
public class BefPrepairController {
    private final IBefPrepairService service;

    @PutMapping("/getBefPrepairQuery")
    @ApiOperation(value = "产前安排查询-可条件查询", tags = "生控部-生控作业-产前计划安排")
    public ResponseInfo getBefPrepairQuery(@RequestBody PrepairRequest request) {
        return service.getBefPrepairQuery(request);
    }

    @PutMapping("/befPrepairUp")
    @ApiOperation(value = "指令更新-更新", tags = "生控部-生控作业-产前计划安排")
    public ResponseInfo befPrepairUp(@RequestParam@ApiParam("1.更新指令 2.更新资料") Integer flag) {
        return service.befPrepairUp(flag);
    }

    @PutMapping("/befPrepairTimeUp")
    @ApiOperation(value = "产前安排更新-更新", tags = "生控部-生控作业-产前计划安排")
    public ResponseInfo befPrepairTimeUp(@RequestBody PrepairTimeRequest request) {
        return service.befPrepairTimeUp(request);
    }

    @GetMapping("/getBefCustom")
    @ApiOperation(value = "品牌-回显", tags = "生控部-生控作业-产前计划安排")
    public ResponseInfo getBefCustom() {
        return service.getBefCustom();
    }


    @GetMapping("/getBefStyle")
    @ApiOperation(value = "型体-回显", tags = "生控部-生控作业-产前计划安排")
    public ResponseInfo getBefStyle() {
        return service.getBefStyle();
    }

    @GetMapping("/getBefLast")
    @ApiOperation(value = "楦头-回显", tags = "生控部-生控作业-产前计划安排")
    public ResponseInfo getBefLast() {
        return service.getBefLast();
    }

    @GetMapping("/getBefPrepair")
    @ApiOperation(value = "作业流程-回显", tags = "生控部-生控作业-产前计划安排")
    public ResponseInfo getBefPrepair() {
        return service.getBefPrepair();
    }

    @PutMapping("/getProdBefExpord")
    @ApiOperation(value = "导出查询资料-导出EXCEL", tags = "生控部-生控作业-产前计划安排")
    public ResponseInfo getProdBefExpord(@RequestBody PrepairRequest request,
                                         @RequestParam@ApiParam("打印产前追踪传1 其他情况打印查询资料") Integer flag,
                                         HttpServletResponse response)
    {
        request= flag==1?new PrepairRequest():request;
        request.setPageSize(-1);
        Object dtos = service.getBefPrepairQuery(request).getData().get("dtos");
        /*选择导出EXCEL的名称和模版名*/
        String templetName = flag==1?"产前追踪模版":"产前计划模版";
        String excelName = flag==1?"产前追踪表_":"产前计划表_";
        excelName+= DateUtils.dateTimeNow();
        try {
            ExcelUtils.creatByTemplate(response,templetName,excelName,dtos);
        } catch (IOException e) {
            return ResponseInfo.error("导出失败");
        }
        return null;
    }

}
